/**
 * Course路由模块
 */

/**
 * @swagger
 * tags:
 *   - name: 课程路由
 *     description: 课程管理相关操作
 * 
 * /courses:
 *   post:
 *     summary: 创建新课程
 *     operationId: createCourse
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/Course'
 *     responses:
 *       '201':
 *         description: 课程创建成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Course'
 *
 * /courses/{id}:
 *   get:
 *     summary: 获取指定ID的课程详情
 *     operationId: getCourseById
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *           format: int64
 *         description: 课程ID
 *     responses:
 *       '200':
 *         description: 成功返回课程详情
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Course'
 *
 * components:
 *   schemas:
 *     Course:
 *       required:
 *         - id
 *         - name
 *         - teacherId
 *         - description
 *         - duration
 *         # 可添加更多必填字段...
 *       type: object
 *       properties:
 *         id:
 *           type: integer
 *           format: int64
 *           example: 100
 *           description: 课程ID
 *         name:
 *           type: string
 *           description: 课程名称
 *         teacherId:
 *           type: integer
 *           format: int64
 *           description: 教师ID
 *         description:
 *           type: string
 *           description: 课程简介
 *         duration:
 *           type: integer
 *           format: int32
 *           description: 课程时长（分钟）
 *         # 其他属性...
 *         category:
 *           type: string
 *           description: 课程类别
 *         createdAt:
 *           type: string
 *           format: date-time
 *           description: 课程创建时间
 *         updatedAt:
 *           type: string
 *           format: date-time
 *           description: 课程最后更新时间
 */


// 引入所需模块
var express = require('express');
var router = express.Router();

// 引入课程相关控制器
const course = require("../controller/course");

const auth = require("../controller/auth");

//守卫路由：所有路由必须执行权限控制的判断
router.all("*", auth.isLogin);

/**
 * @swagger
 * /courses/count:
 *   get:
 *     tags:
 *       - 课程路由
 *     summary: 获取课程总数
 *     description: 返回系统中课程的总数量
 *     responses:
 *       200:
 *         description: "请求成功，返回课程总数"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     total:
 *                       type: integer
 *                       description: 课程总数
 */
router.get("/count", auth.isAdmin, course.getCount);

/**
* @swagger
* /courses/search:
*   get:
*     tags:
*       - 课程路由
*     summary: 根据关键字搜索课程列表信息
*     description: 返回匹配查询关键字的所有课程列表信息
*     parameters:
*       - name: q
*         in: query
*         description: 搜索关键字
*         required: true
*         schema:
*           type: string
*       - name: limit
*         in: query
*         description: 返回结果的数量限制
*         required: false
*         schema:
*           type: integer
*       - name: offset
*         in: query
*         description: 结果集起始位置偏移量
*         required: false
*         schema:
*           type: integer
*     responses:
*       200:
*         description: "请求成功，返回课程列表信息"
*         content:
*           application/json:
*             schema:
*               type: object
*               properties:
*                 code:
*                   type: integer
*                   format: int32
*                 msg:
*                   type: string
*                 data:
*                   type: array
*                   items:
*                     $ref: '#/components/schemas/Course'
*/
router.get("/search", course.search);

/**
 * @swagger
 * /courses/teacher:
 *   get:
 *     tags:
 *       - 课程路由
 *     summary: 获取指定教师ID的课程列表信息
 *     description: 根据教师ID返回该教师的所有课程列表信息
 *     parameters:
 *       - name: teacher_id
 *         in: query
 *         description: 教师ID
 *         required: true
 *         explode: true
 *         schema:
 *           type: integer
 *       - name: order_by
 *         in: query
 *         description: 排序字段（例如：'created_time'、'course_name'等）
 *         required: false
 *         explode: true
 *         schema:
 *           type: string
 *       - name: sort
 *         in: query
 *         description: 排序方式（'asc' 或 'desc'）
 *         required: false
 *         explode: true
 *         schema:
 *           type: string
 *           enum: [asc, desc]
 *       - name: limit
 *         in: query
 *         description: 查询数量（限制返回结果的数量，默认值可由后端设定）
 *         required: false
 *         explode: true
 *         schema:
 *           type: integer
 *       - name: offset
 *         in: query
 *         description: 查询偏移量（分页时使用，默认值为0）
 *         required: false
 *         explode: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 成功获取指定教师的课程列表
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/Course'
 */
router.get("/teacher", course.findAllByTeacherId);

/**
 * @swagger
 * /courses:
 *   post:
 *     tags:
 *       - 课程路由
 *     summary: 新增课程信息
 *     description: 添加新的课程信息并返回新增课程的ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *     security:
 *       - BearerAuth: []
 *     responses:
 *       200:
 *         description: "请求成功，返回新增课程的ID"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: integer
 *                   format: int64
 *                   description: 新增课程的ID
 */
router.post("/", auth.isAdmin, course.add);

/**
 * @swagger
 * /courses/{id}:
 *   delete:
 *     tags:
 *       - 课程路由
 *     summary: 删除指定ID的课程信息
 *     description: 根据课程的内部ID删除课程信息，并返回受影响的记录数
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *           format: int64
 *         description: 待删除课程的内部ID
 *     responses:
 *       200:
 *         description: "请求成功，返回受影响的记录数"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: integer
 *                   description: 受影响的记录数量
 */
router.delete("/:id", auth.isAdmin, course.remove);

/**
 * @swagger
 * /courses/{id}:
 *   put:
 *     tags:
 *       - 课程路由
 *     summary: 修改指定ID的课程信息
 *     description: 更新指定课程的详细信息，并返回受影响的记录数
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *           format: int64
 *         description: 需要更新的课程的内部ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: "#/components/schemas/Course"
 *     responses:
 *       200:
 *         description: "请求成功，返回受影响的记录数"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: integer
 *                   description: 受影响的记录数量
 */
router.put("/:id", auth.isAdmin, course.update);

/**
 * @swagger
 * /courses/{id}:
 *   get:
 *     tags:
 *       - 课程路由
 *     summary: 获取指定ID的课程信息
 *     description: 根据用户内部ID返回课程详细信息
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *           format: int64
 *         description: 要查询的课程的内部ID
 *     responses:
 *       200:
 *         description: "请求成功，返回课程信息"
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Course'
 */
router.get("/:id", course.find);

/**
 * @swagger
 * /courses:
 *   get:
 *     tags:
 *       - 课程路由
 *     summary: 获取课程列表信息
 *     description: 返回系统中的所有课程列表信息，可按需指定查询数量和偏移量
 *     parameters:
 *       - name: limit
 *         in: query
 *         description: 返回结果的数量限制，默认返回全部
 *         required: false
 *         schema:
 *           type: integer
 *       - name: offset
 *         in: query
 *         description: 结果集起始位置偏移量，默认为0
 *         required: false
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: "请求成功，返回课程列表信息"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/User'
 */
router.get("/", course.findAll);

module.exports = router;